References

Import Data

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.4     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
life_expec <- read.csv("data/life_expec.csv")
head(life_expec)
##   Year      Race        Sex Average.Life.Expectancy..Years.
## 1 1900 All Races Both Sexes                            47.3
## 2 1901 All Races Both Sexes                            49.1
## 3 1902 All Races Both Sexes                            51.5
## 4 1903 All Races Both Sexes                            50.5
## 5 1904 All Races Both Sexes                            47.6
## 6 1905 All Races Both Sexes                            48.7
##   Age.adjusted.Death.Rate
## 1                  2518.0
## 2                  2473.1
## 3                  2301.3
## 4                  2379.0
## 5                  2502.5
## 6                  2423.7
colnames(life_expec)
## [1] "Year"                            "Race"                           
## [3] "Sex"                             "Average.Life.Expectancy..Years."
## [5] "Age.adjusted.Death.Rate"
life_expec %>% 
  filter(Year == 2000)
##   Year      Race        Sex Average.Life.Expectancy..Years.
## 1 2000 All Races Both Sexes                            76.8
## 2 2000 All Races     Female                            79.7
## 3 2000 All Races       Male                            74.3
## 4 2000     Black Both Sexes                            71.8
## 5 2000     Black     Female                            75.1
## 6 2000     Black       Male                            68.2
## 7 2000     White Both Sexes                            77.3
## 8 2000     White     Female                            79.9
## 9 2000     White       Male                            74.7
##   Age.adjusted.Death.Rate
## 1                   869.0
## 2                   731.4
## 3                  1053.8
## 4                  1121.4
## 5                   927.6
## 6                  1403.5
## 7                   849.8
## 8                   715.3
## 9                  1029.4

Blank Graph Beginner Mistake in ggplot2

life_expec <- life_expec %>%
  filter(Race == "All Races", Sex == "Both Sexes")

life_expec %>%
  ggplot()

Get the axes using ggplot Grammar of Graphics

life_expec %>%
  ggplot(aes(x = Year, y = Average.Life.Expectancy..Years.))

Specifying Geoms

life_expec %>%
  ggplot(aes(x = Year, y = Average.Life.Expectancy..Years.)) +
  geom_line()

Adding a Title and Axis Labels

life_expec %>% # data layer
  ggplot(aes(x = Year, y = Average.Life.Expectancy..Years.)) + # axes layer
  geom_line() + # geom layer
  labs(  # annotations layer
    title = "United States Life Expectancy: 100 Years of Change",
    y = "Average Life Expectancy (Years)"
  )

From ZevRoss Tutorial

nmmaps<-read.csv("data/chicago-nmmaps.csv", as.is=T)
nmmaps$date<-as.Date(nmmaps$date)
nmmaps<-nmmaps[nmmaps$date>as.Date("1996-12-31"),]
nmmaps$year<-substring(nmmaps$date,1,4)
head(nmmaps)
##      city       date death temp dewpoint      pm10        o3 time season
## 3654 chic 1997-01-01   137 36.0    37.50 13.052268  5.659256 3654 winter
## 3655 chic 1997-01-02   123 45.0    47.25 41.948600  5.525417 3655 winter
## 3656 chic 1997-01-03   127 40.0    38.00 27.041751  6.288548 3656 winter
## 3657 chic 1997-01-04   146 51.5    45.50 25.072573  7.537758 3657 winter
## 3658 chic 1997-01-05   102 27.0    11.25 15.343121 20.760798 3658 winter
## 3659 chic 1997-01-06   127 17.0     5.75  9.364655 14.940874 3659 winter
##      year
## 3654 1997
## 3655 1997
## 3656 1997
## 3657 1997
## 3658 1997
## 3659 1997
g<-ggplot(nmmaps, aes(date, temp))+geom_point(color="firebrick")
g

g<-g+ggtitle('Temperature')
g

g+theme(plot.title = element_text(size=20, face="bold", 
    margin = margin(10, 0, 10, 0)))

### Additional Font Library

library(extrafont)
## Registering fonts with R
g+theme(plot.title = element_text(size=30,lineheight=.8, 
  vjust=1,family="Bauhaus 93"))

g<-g+ggtitle("This is a longer\ntitle than expected")
g+theme(plot.title = element_text(size=20, face="bold", vjust=1, lineheight=0.6))

### Working with Legends

g<-ggplot(nmmaps, aes(date, temp, color=factor(season)))+geom_point()
g

### Turn off the Legend Title

g+theme(legend.title=element_blank())

Change the style of Legend Title

g+theme(legend.title = element_text(colour="chocolate", size=16, face="bold"))

### Change the Title of Legend

g+theme(legend.title = element_text(colour="chocolate", size=16, face="bold"))+
  scale_color_discrete(name="This color is\ncalled chocolate!?")

### Change the background box in legend

g+theme(legend.key=element_rect(fill='pink'))

### Change the size of symbol in the legend only

g+guides(colour = guide_legend(override.aes = list(size=6)))

Leave the Layer off the Legend

g+geom_text(data=nmmaps, aes(date, temp, label=round(temp)), size=4)

## Convert ggplot2 plot to Plotly

Dependencies

#install.packages("devtools")
#library("devtools")
#install_github("ropensci/plotly")
g <- ggplot(nmmaps, aes(date, temp, color=factor(season)))+  geom_point() +   
  scale_color_manual(values=c("dodgerblue4", "darkolivegreen4",  
                              "darkorchid3", "goldenrod1"))
g

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
g_plotly <- ggplotly(g)
g_plotly